package com.demo.gulimall.cart.controller;

import com.demo.gulimall.cart.service.CartService;
import com.demo.gulimall.cart.vo.*;
import com.demo.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Chen Yuan
 * @create 2023-06-19  14:00
 */
@Slf4j
@RestController
@RequestMapping("/cart")
public class CartController {
	@Autowired
	private CartService cartService;

	@GetMapping("/listpage")
	public R cartListPage(@RequestParam("userId") Long userId){

		CartVo cart = cartService.getCartList(userId);

		return R.ok(cart);
	}


	/**
	 * 加入购物车
	 * @param skuId
	 * @param num 数量
	 */
	@GetMapping("/addtocart")
	public R addToCart(@RequestParam("skuId") Long skuId,
	                   @RequestParam("num") Integer num){

		CartItemVo cartItem = cartService.addToCart(skuId, num);

		return R.ok(cartItem);
	}

	/**
	 * 删除购物车商品
	 * @param skuId
	 */
	@GetMapping("/deleteitem")
	public R deleteCartItem(@RequestParam("skuId") Long skuId){
		cartService.deleteCartItem(skuId);
		return R.ok();
	}


	@GetMapping("/getCheckedItem")
	public R<List<CartItemVo>> getCheckedCartItemsByUserId(@RequestParam("userId") Long userId){
		List<CartItemVo> cartItems = cartService.getCheckedCartItemsByUserId(userId);
		return R.ok(cartItems);
	}
}
